package org.example;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class MarkDownOperation {
    public String addHead(String content, int level) {

        content = " " + content;
        for (int i = 0; i < level; i++) {
            content = "#" + content;
        }

        return content + "\n\r";
    }

    public String addParagraph(String content) {
        return "\n\r" + content + "\n\r";
    }

    public String addLineBreak() {
        return "&lt;br&gt;";
    }

    public String addBold(String content) {
        return "***" + content + "***";
    }

    public String addItalic(String content) {
        return "*" + content + "*";
    }

    public String addBoldAndItalic(String content) {
        return "***" + content + "***";
    }

    public String addQuote(String content) {
        return "> " + content;
    }

    public String addUnOrderedList(String... strings) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            sb.append("- " + strings[i] + "\n\r");
        }
        return sb.toString();
    }

    public String addOrderedList(String... strings) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            sb.append((i + 1) + ". " + strings[i] + "\n\r");
        }
        return sb.toString();
    }

    public String addCodeStatement(String content) {
        return "`" + content + "`";
    }

    public String addCodeBlock(String content, String language) {
        return "```" + language + "\n\r" + content + "\n\r" + "```";
    }

    public String addDividingLine() {
        return "---\n\r";
    }

    public String addLink(String title, String url) {
        return "[" + title + "](" + url + ")";
    }

    public String addPic(String title, String url) {
        return "![" + title + "](" + url + ")";
    }

    public String addForm(List<List<String>> content, List<String> filed) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < filed.size(); i++) {
            sb.append("| " + filed.get(i) + " ");
        }
        sb.append("|\n\r");

        for (int i = 0; i < filed.size(); i++) {
            sb.append("| ---- ");
        }
        sb.append("|\n\r");

        for (int j = 0; j < content.size(); j++) {
            List<String> record = content.get(j);
            for (int i = 0; i < record.size(); i++) {
                sb.append("| " + record.get(i) + " ");
            }
            sb.append("|\n\r");
        }

        return sb.toString();
    }

    public String addDeleteLine(String content) {
        return "~~" + content + "~~";
    }


    public String addTaskList(List<String> list, List<Boolean> flag) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            char x = flag.get(i) ? 'x' : ' ';
            sb.append("- [" + x + "] " + list.get(i));
        }
        return sb.toString();
    }

    private static String toMarkDownDoc(ClassInfo classInfo, AbstractMarkDownTemplate abstractMarkDownTemplate) {
        String[] markdown = abstractMarkDownTemplate.template(classInfo, new MarkDownOperation());
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < markdown.length; i++) {
            stringBuilder.append(markdown[i]);
        }
        return stringBuilder.toString();
    }

    public static String toMarkDownDoc(String srcPath, AbstractMarkDownTemplate abstractMarkDownTemplate) {
        return toMarkDownDoc(ClassInfo.getClassInfo(srcPath),abstractMarkDownTemplate);
    }

    public static void toMarkDownDocFile(String srcPath, String desPath, AbstractMarkDownTemplate template) {
        String s = toMarkDownDoc(ClassInfo.getClassInfo(srcPath), template);
        try (FileWriter fileWriter = new FileWriter(desPath)) {
            fileWriter.write(s);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
